<template>
  <div>
        <body id="paper">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      class="login-container"
      label-position="left" label-width="0px"
    >
    <h3 class="login_title">登录</h3>
    <el-form-item prop="tusername">
      <el-input type="text" v-model="ruleForm.tusername"
                auto-complete="off" placeholder="用户名"></el-input>
    </el-form-item>
      <el-form-item prop="tpassword">
      <el-input type="password" v-model="ruleForm.password"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
      <el-form-item prop="checkPass">
      <el-input type="password" v-model="ruleForm.checkPass"
                auto-complete="off" placeholder="确认密码"></el-input>
    </el-form-item>

      <el-form-item>
        <el-button type="primary"  style="width: 100%;background: #1890ff;border: none;" @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
    </body>
  </div>
</template>

<script>
// import Center from '@/components/Center'
// import {login} from "@/services/UserService"
export default {
    components:{
    },
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        tusername:"",
        password: "",
        checkPass: "",
      },
      rules: {
        tusername: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // login(this.ruleForm).then((resp)=>{
          //   console.log("===========");
          //   console.log(resp);
          // })

          this.$store.dispatch("userStore/fetchLogin",this.ruleForm);
          alert("登录成功!");
          this.$router.push({name:"Menu"})

        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
  #paper {
    /*background:url("../assets/img/bg/eva1.jpg") no-repeat;*/
    background-position: center;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: -5px 0px;
  }
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 160px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }
  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
  .login_remember {
    margin: 0px 0px 35px 0px;
    text-align: left;
  }
</style>